package com.quanyan.place.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.mail.EmailUtils;
import com.quanyan.common.utils.BeanCopierUtils;
import com.quanyan.common.utils.UUIDUtils;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.*;
import com.quanyan.place.entity.apireq.RespPlaceScale;
import com.quanyan.place.entity.apiresp.RespCategoryAndScale;
import com.quanyan.place.entity.apiresp.RespPlaceInfo;
import com.quanyan.place.entity.vo.PlaceCategoryVo;
import com.quanyan.place.enums.PlaceCategoryEnum;
import com.quanyan.place.mapper.*;
import com.quanyan.place.service.PlaceBaseService;
import com.quanyan.place.wrapper.PlaceWrapper;
import org.apache.commons.collections.list.TreeList;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 场馆基础service实现
 * Created by yangyun on 16-6-6.
 */
@Service("placeBaseService")
public class PlaceBaseServiceImpl implements PlaceBaseService {

    @Autowired
    TbBizPlaceMapper placeMapper;

    @Autowired
    TbBizPlaceUnitMapper placeUnitMapper;

    @Autowired
    TbBizPlaceUnitCombineMapper placeUnitCombineMapper;

    @Autowired
    TbBizPlaceAttachMapper tbBizPlaceAttachMapper;

    @Autowired
    TbBizPlaceCategoryMapper tbBizPlaceCategoryMapper;

    @Autowired
    CagetoryMapper categoryMapper;

    @Autowired
    DictMapper dictMapper;

    @Autowired
    VPlaceMapper vPlaceMapper;

    @Autowired
    TbBizPlacePrintMapper tbBizPlacePrintMapper;

    private final static String placeAdminUser = "yun.yang@quncaotech.com";

    /**
     * 获取场馆下场地列表
     *
     * @param placeId
     * @param categoryId 运动项目
     * @param isSign     1：已签约 0：未签约
     * @return
     */
    @Override
    public List<TbBizPlaceUnit> getPlaceUnitByPlaceId(Integer placeId, Byte categoryId, boolean isSign) {
        TbBizPlaceUnitExample example = new TbBizPlaceUnitExample();
        TbBizPlaceUnitExample.Criteria criterial = example.createCriteria();
        if (placeId != null) {
            criterial.andPlaceIdEqualTo(placeId);
        }
        if (categoryId != null) {
            criterial.andCategoryIdEqualTo(categoryId);
        }
        if (isSign) {
            criterial.andIsSignEqualTo(Constants.BYTE_ONE);  // 已签约
        }
        // 获取场馆下已签约场地列表
        List<TbBizPlaceUnit> placeUnitList = placeUnitMapper.selectByExample(example);
        return placeUnitList;
    }

    /**
     * 获取场馆下场地列表(包括签约的基本场和 组合场)
     *
     * @param placeId
     * @param categoryId 运动项目
     * @param isSign     1：已签约 0：未签约
     * @return
     */
    @Override
    public List<TbBizPlaceUnit> getPlaceUnitByPlaceIdEx(Integer placeId, Byte categoryId, boolean isSign) {
        TbBizPlaceUnitExample example = new TbBizPlaceUnitExample();
        TbBizPlaceUnitExample.Criteria criterial = example.createCriteria();
        if (placeId != null) {
            criterial.andPlaceIdEqualTo(placeId);
        }
        if (categoryId != null) {
            criterial.andCategoryIdEqualTo(categoryId);
        }

        if (isSign) {
            criterial.andIsSignEqualTo(Constants.BYTE_ONE);  // 已签约
        }

        // 获取场馆下已签约场地列表
        List<TbBizPlaceUnit> placeUnitList = placeUnitMapper.selectByExample(example);

        TbBizPlaceUnitCombineExample example2 = new TbBizPlaceUnitCombineExample();
        TbBizPlaceUnitCombineExample.Criteria criterial2 = example2.createCriteria();
        if (placeId != null) {
            criterial2.andPlaceIdEqualTo(placeId);
        }
        if (categoryId != null) {
            criterial2.andCategoryIdEqualTo(categoryId);
        }
        criterial2.andIsDeleteEqualTo(Constants.BYTE_ZERO);  // 未被删除

        // 获取场馆下有效的组合场地
        List<TbBizPlaceUnitCombine> placeUnitCombineList = placeUnitCombineMapper.selectByExample(example2);
        if (placeUnitCombineList != null && placeUnitCombineList.size() > 0) {
            for (TbBizPlaceUnitCombine tbPUC : placeUnitCombineList) {
                TbBizPlaceUnit tbPU = new TbBizPlaceUnit();
                BeanUtils.copyProperties(tbPUC, tbPU);
                placeUnitList.add(tbPU);
            }
        }

        return placeUnitList;
    }

    /**
     * 根据场馆ID获取场馆附属信息
     *
     * @param placeId
     * @return
     */
    public TbBizPlaceAttach getBizPlaceAttach(Integer placeId) {
        TbBizPlaceAttachExample example = new TbBizPlaceAttachExample();
        TbBizPlaceAttachExample.Criteria criterial = example.createCriteria();
        if (placeId != null) {
            criterial.andPlaceIdEqualTo(placeId);
        }
        List<TbBizPlaceAttach> result = tbBizPlaceAttachMapper.selectByExample(example);
        if (result != null && result.size() > 0) {
            return result.get(0);
        } else {
            return null;
        }
    }

    /**
     * 根据vipshareNo查询所关联的场馆
     * @param vipShareNo
     * @return
     */
    public List<TbBizPlaceAttach> getBizPlaceAttachList(String vipShareNo) {
        TbBizPlaceAttachExample example = new TbBizPlaceAttachExample();
        TbBizPlaceAttachExample.Criteria criterial = example.createCriteria();
        if (!org.springframework.util.StringUtils.isEmpty(vipShareNo)) {
            criterial.andVipShareNoEqualTo(vipShareNo);
        }
        List<TbBizPlaceAttach> result = tbBizPlaceAttachMapper.selectByExample(example);
        return result;
    }

    /**
     * 根据场馆ID获取场馆信息表数据
     *
     * @param placeId
     * @return
     */
    public TbBizPlace getPlaceById(Integer placeId) {
        return placeMapper.selectByPrimaryKey(placeId);
    }

    /**
     * 根据场馆Id获取经营范围
     *
     * @param placeId
     * @return
     */
    @Override
    public List<TbBizPlaceCategory> getPlaceCategoryList(Integer placeId) {
        TbBizPlaceCategoryExample tbBizPlaceCategoryExample = new TbBizPlaceCategoryExample();
        TbBizPlaceCategoryExample.Criteria tbBizPlaceCategoryCriteria = tbBizPlaceCategoryExample.createCriteria();
        tbBizPlaceCategoryCriteria.andPlaceIdEqualTo(placeId);
        List<TbBizPlaceCategory> tbBizPlaceCategoryList = tbBizPlaceCategoryMapper.selectByExample(tbBizPlaceCategoryExample);
        return tbBizPlaceCategoryList;
    }

    /**
     * 获取运动类别信息
     *
     * @return
     */
    @Override
    public APIResponse getCategory() {
        CagetoryExample cagetoryExample = new CagetoryExample();
        CagetoryExample.Criteria cagetoryCriteria = cagetoryExample.createCriteria();
        cagetoryCriteria.andIsEnableEqualTo(Constants.YES);
        cagetoryExample.setOrderByClause("id");
        List<Cagetory> list = categoryMapper.selectNameByExample(cagetoryExample);
        return APIResponse.returnSuccess(list);
    }

    @Override
    public APIResponse selectPlaceByName(String placeName) {
        TbBizPlaceExample tbBizPlaceExample = new TbBizPlaceExample();
        TbBizPlaceExample.Criteria criteria = tbBizPlaceExample.createCriteria();
        criteria.andNameLike("%" + placeName + "%");
        criteria.andSourceIdEqualTo(2);
        criteria.andIsSignEqualTo(Constants.BYTE_ONE);
        criteria.andIsDeleteEqualTo(0);
        return APIResponse.returnSuccess(placeMapper.selectByExample(tbBizPlaceExample));
    }

    @Override
    public PageObj<List<TbBizPlace>> getPlaceByLikeName(String placeName, Integer isDelete) {
        Integer pageNum = 0;
        Integer pageSize = Constants.DEFAULT_PAGE_SIZE;
        TbBizPlaceExample tbBizPlaceExample = new TbBizPlaceExample();
        TbBizPlaceExample.Criteria criteria = tbBizPlaceExample.createCriteria();
        criteria.andNameLike("%" + placeName.trim() + "%");
        criteria.andIsDeleteEqualTo(isDelete);
        int total = placeMapper.countByExample(tbBizPlaceExample);
       // tbBizPlaceExample.setPage(new Page(pageNum * pageSize, pageSize, total));
        List<TbBizPlace> list = placeMapper.selectByExample(tbBizPlaceExample);
        PageObj<List<TbBizPlace>> result = PageObj.create(total, pageNum, pageSize, list);
        return result;
    }

    @Override
    public TbBizPlace getPlaceByEqualName(String placeName, Integer isDelete) {
        TbBizPlaceExample tbBizPlaceExample = new TbBizPlaceExample();
        TbBizPlaceExample.Criteria criteria = tbBizPlaceExample.createCriteria();
        criteria.andNameEqualTo(placeName);
        criteria.andIsDeleteEqualTo(0);
        List<TbBizPlace> placeList = placeMapper.selectByExample(tbBizPlaceExample);
        if (placeList != null && placeList.size() > 0) {
            return placeList.get(0);
        } else {
            return null;
        }
    }

    /**
     * 生成场馆编号
     * @param cityCode
     * @param placeId
     * @return
     */
    @Override
    public String generatePlaceCode(String cityCode, Integer placeId, Set<Integer> categoryIdSet) {
        String idStr = placeId.toString();
        String categoryStr = "";
        if (null != categoryIdSet && !categoryIdSet.isEmpty()) {
            if (categoryIdSet.size() > 1) {
                // 综合场馆
                categoryStr = "ZH";
            } else {
                Iterator<Integer> it = categoryIdSet.iterator();
                Integer catergoryId = it.next();
                if (null != catergoryId) {
                    if (null != PlaceCategoryEnum.getEnum(catergoryId)) {
                        categoryStr = PlaceCategoryEnum.getEnum(catergoryId).getCode();
                    }
                }
            }
        }
        while (idStr.length() < 6){
            idStr = "0" + idStr;
        }
        return cityCode + categoryStr + idStr;
    }

    @Override
    public void updatePlaceBySelective(TbBizPlace tbBizPlace) {
        placeMapper.updateByPrimaryKeySelective(tbBizPlace);
    }


    @Override
    public void updatePlaceAttachBySelective(TbBizPlaceAttach tbBizPlaceAttach) {
        tbBizPlaceAttachMapper.updateByPrimaryKeySelective(tbBizPlaceAttach);
    }

    @Override
    public boolean updateByPrimaryKeySelective(TbBizPlace tbBizPlace) {
        int flag = placeMapper.updateByPrimaryKeySelective(tbBizPlace);
        return flag == 1;
    }

    @Override
    public void deletePlaceCategory(Integer id) {
        tbBizPlaceCategoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insertPlaceCategory(TbBizPlaceCategory tbBizPlaceCategory) {
        tbBizPlaceCategoryMapper.insert(tbBizPlaceCategory);
    }

    /**
     * 判断指定Id的场馆是否存在
     * @param placeId
     * @return
     */
    @Override
    public boolean isExistsPlace(Integer placeId) {
        TbBizPlace bizPlace = placeMapper.selectByPrimaryKey(placeId);
        return null != bizPlace ? true : false;
    }

    @Override
    public void updatePlaceUnitBySelective(TbBizPlaceUnit tbBizPlaceUnit) {
        TbBizPlaceUnitExample  example =new TbBizPlaceUnitExample();
        TbBizPlaceUnitExample.Criteria criteria=example.createCriteria();
        if (tbBizPlaceUnit.getPlaceId()!=null){
            criteria.andPlaceIdEqualTo(tbBizPlaceUnit.getPlaceId());
        }
        if (tbBizPlaceUnit.getId()!=null){
            criteria.andIdEqualTo(tbBizPlaceUnit.getId());
        }
        TbBizPlaceUnit vo=new TbBizPlaceUnit();
        vo.setIsSign(tbBizPlaceUnit.getIsSign());
        placeUnitMapper.updateByExampleSelective(vo, example);
    }

    public void updatePlaceUnitByPrimaryKey(TbBizPlaceUnit tbBizPlaceUnit){
        placeUnitMapper.updateByPrimaryKey(tbBizPlaceUnit);
    }

    /**
     * 获取场馆运动项目及场地规模
     * @param placeId
     * @return
     */
    public List<RespCategoryAndScale> getCategoryAndScale(Integer placeId, boolean isFilterNotPriced){

        List<RespCategoryAndScale> categoryAndScaleList = new ArrayList<RespCategoryAndScale>();
        // 获取场馆经营运动项目
        List<TbBizPlaceCategory> tbBizPlaceCategoryList = this.getPlaceCategoryList(placeId);
        // 已签约场地列表[注意组合场在unit表中是"虚拟的"其中的isSign标志为0]
        List<TbBizPlaceUnit> placeUnitList = getPlaceUnitByPlaceIdEx(placeId, null, true);

        // 获取数据字典中的场地规模
        //Map<String,String> placeScaleDict = this.getPlaceScale();
        List<RespPlaceScale> allPlaceScale = this.getPlaceScaleEx();

        for (TbBizPlaceCategory placeCategory : tbBizPlaceCategoryList){
            if (isFilterNotPriced){
                // 过滤未定价的
                Integer count = vPlaceMapper.countSalePlanMain(placeId, placeCategory.getCategoryId());
                if (null == count || count == 0)
                    continue;
            }
            RespCategoryAndScale categoryAndScale = new RespCategoryAndScale();
            //categoryAndScale.setPlaceId(placeId);
            categoryAndScale.setId(placeCategory.getCategoryId());
            if (null != PlaceCategoryEnum.getEnum(placeCategory.getCategoryId())){
                categoryAndScale.setName(PlaceCategoryEnum.getEnum(placeCategory.getCategoryId()).getLabel());
            }
            // 场地规模信息封装  只有足球和篮球有场地规模
            if (PlaceCategoryEnum.CATEGORY_ZQ.getId() == placeCategory.getCategoryId() ||
                    PlaceCategoryEnum.CATEGORY_LQ.getId() == placeCategory.getCategoryId()){
//                List<RespPlaceScale>  scaleList = this.packagePlaceScale(placeCategory.getCategoryId(), placeScaleDict,
//                        placeUnitList);

                List<RespPlaceScale>  scaleList = this.packagePlaceScaleEx(placeCategory.getCategoryId(), allPlaceScale,
                        placeUnitList);
                categoryAndScale.setRespPlaceScales(scaleList);
            }
            categoryAndScaleList.add(categoryAndScale);
        }
        return categoryAndScaleList;
    }



    /**
     * 按项目封装场地规模
     * @param categoryId
     * @param dictMap
     * @param placeUnitList
     * @return
     */
    private List<RespPlaceScale> packagePlaceScale(Integer categoryId,
                                                  Map<String,String> dictMap,
                                                  List<TbBizPlaceUnit> placeUnitList) {
        List<RespPlaceScale> list = new ArrayList(); // 场地规模  如key:1801 value:三人制
        if (null != categoryId){
            for (TbBizPlaceUnit unit : placeUnitList) {
                RespPlaceScale respPlaceScale = new RespPlaceScale();
                if (null != unit.getCategoryId()) {
                    if (categoryId != unit.getCategoryId().intValue()) {
                        // 项目不匹配，跳出循环
                        continue;
                    }
                    for (RespPlaceScale placeScale : list) {
                       if (placeScale.getScaleId().equals(unit.getFieldScale())){
                            continue;
                        }
                    }
                   /* if (dictMap.containsKey(unit.getFieldScale())) {
                        // 已经放入map，跳出循环
                        continue;
                    }*/
                    if (null != dictMap && dictMap.containsKey(unit.getFieldScale())) {
                            respPlaceScale.setScaleId(unit.getFieldScale());
                            respPlaceScale.setScaleName(dictMap.get(unit.getFieldScale()));
                            list.add(respPlaceScale);
                    }
                }
            }
        }
        List<RespPlaceScale> listWithoutDup1 = new ArrayList<>(new HashSet<>(list));
        return listWithoutDup1;
    }

    /**
     * 按项目封装场地规模
     * @param categoryId
     * @param allScaleList
     * @param placeUnitList
     * @return
     */
    private List<RespPlaceScale> packagePlaceScaleEx(Integer categoryId,
                                                     List<RespPlaceScale> allScaleList,
                                                   List<TbBizPlaceUnit> placeUnitList) {
        List<RespPlaceScale> list = new ArrayList(); // 场地规模  如key:1801 value:三人制

        if (null != categoryId){
            for (RespPlaceScale scale : allScaleList) {
                for (TbBizPlaceUnit unit : placeUnitList) {
                    RespPlaceScale respPlaceScale = new RespPlaceScale();
                    if (null != unit.getCategoryId()) {
                        if (categoryId != unit.getCategoryId().intValue()) {
                            // 项目不匹配，跳出循环
                            continue;
                        }

                        String unitScale = unit.getFieldScale();
                        if (unitScale != null && unitScale.equals(scale.getScaleId())) {
                            // 如果已经插入进不要在插入了
                            boolean hasInsert = false;
                            for (RespPlaceScale placeScale : list) {
                                if (placeScale.getScaleId().equals(unitScale)){
                                    hasInsert = true;
                                    break;
                                }
                            }

                            if (!hasInsert) {
                                list.add(scale);
                            }
                        }
                    }
                }
            }
        }

        return list;
    }

    /**
     * 场地规模数据字典
     * @return
     */
    public Map<String,String> getPlaceScale(){
        DictExample example = new DictExample();
        DictExample.Criteria criteria = example.createCriteria();
        List<String> tagGroupString = new ArrayList<>();
        tagGroupString.add(PlaceConstants.DICT_TYPE_18);
        tagGroupString.add(PlaceConstants.DICT_TYPE_31);
        criteria.andTagGroupIn(tagGroupString);
        Map<String,String> scaleMap = new HashMap<String,String>();
        List<Dict> dictList = dictMapper.selectByExample(example);
        for (Dict dict : dictList){
            if (dict != null && dict.getDataKey() != null
                    && dict.getDataValue() != null){
                scaleMap.put(dict.getDataKey(),dict.getDataValue());
            }
        }
        return scaleMap;
    }


    /**
     * 场地规模数据字典,按数据库中顺序返回
     * @return
     */
    public List<RespPlaceScale> getPlaceScaleEx(){
        DictExample example = new DictExample();
        DictExample.Criteria criteria = example.createCriteria();
        List<String> tagGroupString = new ArrayList<>();
        tagGroupString.add(PlaceConstants.DICT_TYPE_18);
        tagGroupString.add(PlaceConstants.DICT_TYPE_31);
        criteria.andTagGroupIn(tagGroupString);

        List<RespPlaceScale> scaleList = new ArrayList<>();
        List<Dict> dictList = dictMapper.selectByExample(example);
        for (Dict dict : dictList){
            if (dict != null && dict.getDataKey() != null
                    && dict.getDataValue() != null){
                RespPlaceScale scale = new RespPlaceScale();
                scale.setScaleId(dict.getDataKey());
                scale.setScaleName(dict.getDataValue());
                scaleList.add(scale);
            }
        }
        return scaleList;
    }

    public boolean isAnyOrder(Integer uid) {
        Integer orderCount = vPlaceMapper.countPlaceOrder(uid, Constants.BYTE_ONE);
        return (null != orderCount && orderCount >0) ? true : false;
    }

    /**
     * 获得场馆
     * @param businessMode
     * @param isDelete
     * @param isShelves
     * @return
     */
    @Override
    public List<TbBizPlace> getPoolPlace(Byte businessMode,Integer isDelete,Integer isShelves) {
        TbBizPlaceExample example =new TbBizPlaceExample();
        TbBizPlaceExample.Criteria criteria=example.createCriteria();
        criteria.andBusinessModeEqualTo(businessMode);
        criteria.andIsDeleteEqualTo(isDelete);
        criteria.andIsSignEqualTo((byte)1);
       // criteria.andIsShelvesEqualTo(isShelves);
        List<TbBizPlace> placeList=placeMapper.selectByExample(example);
        return placeList;
    }

    /**
     * 保存更新打印机设置
     *
     * @param placeId
     * @param printNum
     * @param printMemo
     * @return
     */
    @Override
    public APIResponse<?> saveOrUpdatePrintSetting(Integer placeId, Integer printNum, String printMemo) {
        if (placeId == null){
            return APIResponse.returnFail("placeId不能空");
        }
        TbBizPlacePrint tbBizPlacePrint = new TbBizPlacePrint();
        tbBizPlacePrint.setPlaceId(placeId);
        tbBizPlacePrint.setPrintNum(printNum);
        tbBizPlacePrint.setPrintMemo(printMemo);
        try{
            tbBizPlacePrintMapper.insertSelective(tbBizPlacePrint);
        }catch (Exception e){
            TbBizPlacePrintExample tbBizPlacePrintExample = new TbBizPlacePrintExample();
            tbBizPlacePrintExample.createCriteria().andPlaceIdEqualTo(placeId);
            tbBizPlacePrintMapper.updateByExampleSelective(tbBizPlacePrint,tbBizPlacePrintExample);
        }
        return APIResponse.returnSuccess();
    }

    /**
     * 设置场馆运动项目最小销售时间单元（
     * 0：半小时  1:1小时  2:2小时
     * @param placeId
     * @param categoryId
     * @param timeUnit
     */
    @Override
    public boolean setPlaceCategorySaleTimeUnit(Integer placeId, Integer categoryId, Byte timeUnit, Byte startSaleTimeType) {
        TbBizPlaceCategoryExample example =new TbBizPlaceCategoryExample();
        TbBizPlaceCategoryExample.Criteria criteria=example.createCriteria();
        criteria.andPlaceIdEqualTo(placeId);
        if (null != categoryId){
            criteria.andCategoryIdEqualTo(categoryId);
        }
        TbBizPlaceCategory placeCategory = new TbBizPlaceCategory();
        if (null != timeUnit){
            placeCategory.setMinSaleTimeUnit(timeUnit);
        } else {
            // 默认1小时
            placeCategory.setMinSaleTimeUnit(Constants.BYTE_ONE);
        }

        if (null != startSaleTimeType){
            placeCategory.setStartSaleTimeType(startSaleTimeType);
        } else {
            // 默认整点起售
            placeCategory.setStartSaleTimeType(Constants.BYTE_ONE);
        }
        int ret = tbBizPlaceCategoryMapper.updateByExampleSelective(placeCategory, example);
        return ret > 0 ? true : false;
    }

    /**
     * 获取场馆项目销售最小单元
     * @param placeId
     * @return
     */
    @Override
    public List<TbBizPlaceCategory> getPlaceCategorySaleTimeUnit(Integer placeId) {
        return this.getPlaceCategoryList(placeId);
    }

    @Override
    public List<TbBizPlaceAttach> getSharePlaceVipByShareNo(String vipShareNo) {
        TbBizPlaceAttachExample example = new TbBizPlaceAttachExample();
        TbBizPlaceAttachExample.Criteria criterial = example.createCriteria();
        if (StringUtils.isNotEmpty(vipShareNo)) {
            criterial.andVipShareNoEqualTo(vipShareNo);
        }
        List<TbBizPlaceAttach> result = tbBizPlaceAttachMapper.selectByExample(example);
        return result;
    }

    /**
     * 根据场馆Id及共享No查询关联场馆
     * @param placeId
     * @param vipShareNo
     * @return
     */
    @Override
    public List<RespPlaceInfo> getSharePlace(Integer placeId, String vipShareNo) {
        List<RespPlaceInfo> result = vPlaceMapper.getShareVipPlaceList(placeId, vipShareNo, true);
        return result;
    }

    /**
     * 根据场馆id和运动项目查询场馆项目配置
     * @param placeId
     * @param categoryId
     * @return
     */
    @Override
    public TbBizPlaceCategory getPlaceCategory(Integer placeId, Integer categoryId) {
        TbBizPlaceCategoryExample tbBizPlaceCategoryExample = new TbBizPlaceCategoryExample();
        TbBizPlaceCategoryExample.Criteria tbBizPlaceCategoryCriteria = tbBizPlaceCategoryExample.createCriteria();
        tbBizPlaceCategoryCriteria.andPlaceIdEqualTo(placeId);
        tbBizPlaceCategoryCriteria.andCategoryIdEqualTo(categoryId);
        List<TbBizPlaceCategory> tbBizPlaceCategoryList = tbBizPlaceCategoryMapper.selectByExample(tbBizPlaceCategoryExample);
        if (null != tbBizPlaceCategoryList && tbBizPlaceCategoryList.size() > 0){
            return tbBizPlaceCategoryList.get(0);
        }
        return null;
    }

    /**
    * 根据场馆id获取打印机设置
    */
    public APIResponse<?> getPrintSettingByPlaceId(Integer placeId) {
        if (placeId == null) {
            return APIResponse.returnFail("placeId不能空");
        }
        TbBizPlacePrintExample tbBizPlacePrintExample = new TbBizPlacePrintExample();
        tbBizPlacePrintExample.createCriteria().andPlaceIdEqualTo(placeId);
        List<TbBizPlacePrint> tbBizPlacePrintList = tbBizPlacePrintMapper.selectByExample(tbBizPlacePrintExample);
        if (com.alibaba.dubbo.common.utils.CollectionUtils.isNotEmpty(tbBizPlacePrintList)) {
            return APIResponse.returnSuccess(tbBizPlacePrintList.get(0));
        }
        return APIResponse.returnSuccess();
    }

    /**
     * 指定场馆、栏目是否已定价
     * @param placeId
     * @param categoryId
     * @return
     */
    public boolean isAnyPrice(Integer placeId, Integer categoryId){
        Integer count = vPlaceMapper.countSalePlanMain(placeId, categoryId);
        return (null != count && count >0) ? true : false;
    }

    /**
     * 发送Email给管理员
     * @param subject
     * @param content
     */
    @Override
    @Async
    public void sendTextEmails(String subject, String content) {
        EmailUtils.getInstance().sendTextEmails(placeAdminUser, subject, content);
    }
}
